/**
 * 主功能页面，用于包括所有页面元素并加载显示其它页面部分
 *    主要还用于解析公共数据，用于其它页面数据准备工作
 * author:zhushy
 * date:2016年8月22日22:30:42
 */
var index = {
	/**
	 * bindEvent 页面事件
	 */
	bindEvent: function () {
		$("#rtn_index").unbind().on("click", function () {
			summer.closeWin();
		});
		
		//搜索跳转
		$("#searchInput_index").unbind().on("click", function () {
			window.sessionStorage.setItem("clear_input", true);
			window.sessionStorage.removeItem("searchKey");
			//显示返回键
			window.sessionStorage.searchKeyNotBackFlag = "true";
			//标题的显示
			window.sessionStorage.pageTitle = "搜索";
			window.location.href = "#productList/search";
		});
		
		//行业图表跳转
		$("#selectIndustry_index").unbind().on("click", function () {
			//标题的显示
			window.sessionStorage.pageTitle = "行业图表";
			window.location.href = "#selectIndustry";
		});
		
		//产品
		$("#productList_index").unbind().on("click", function () {
			window.sessionStorage.setItem("clear_input", true);
			window.sessionStorage.removeItem("searchKey");
			//显示返回键
			window.sessionStorage.searchKeyNotBackFlag = "true";
			//标题的显示
			window.sessionStorage.pageTitle = "产品";
			window.location.href = "#productList/searchProdcut";
		});

		//价值文档
		$("#worthDoc_index").unbind().on("click", function () {
			window.sessionStorage.setItem("clear_input", true);
			window.sessionStorage.removeItem("searchKey");
			//显示返回键
			window.sessionStorage.searchKeyNotBackFlag = "true";
			//标题的显示
			window.sessionStorage.pageTitle = "价值文档";
			window.location.href = "#productList/searchWorthdoc";
		});
		
		//跳到收藏
		//$("#collectionList_index").unbind().on("click", function () {
		//	//跳转
		//	window.location.href = "#collectList";
		//});

		
		//附件查看
		//$("#attenchmentList_index").unbind().on("click",function(){
		//	window.location.href = "#attachmentList";
		//});

		//S-OEM
		$("#collectionList_index").unbind().on("click",function(){
			window.sessionStorage.setItem("clear_input", true);
			window.sessionStorage.removeItem("searchKey");
			//显示返回键
			window.sessionStorage.searchKeyNotBackFlag = "true";
			//标题的显示
			window.sessionStorage.pageTitle = "S-OEM";
			window.location.href = "#productList/searchSOEM";
		});

		//我的
		$("#attenchmentList_index").unbind().on("click",function(){
			window.location.href = "#mine";
		});

		// 润滑油百科跳转
		$("#baike_index").unbind().on("click",function(){
			if(common.getClient()=='ios'){
				summer.openWin({
					id: 'baikeWeb',
					showProgress:"true",
					url: 'http://shell-product.rowchina.cn/qa/',
					type:'containner'
				});
			}else{
				window.location.href="#baike";
			}
			//window.location.href="#baike";
		});

		// 经销商平台跳转
		$("#pingtai_index").unbind().on("click",function(){
			//window.location.href="#pingtai";
			summer.openWin({
				id: 'pingtaiWeb',
				showProgress:"true",
				url: 'http://www.shell.com.cn/va-d',
				type:'containner'
			});
		});
	},
	/**
	 * 进行页面路由监听配置
	 * 
	 */
	bindRout: function () {
		//默认空时候进入首页（行业选择界面）
		Path.map("#selectIndustry").to(function () {
			//记录状态
			common.loadPageDiv("html/selectIndustry.html", true);
		});

		//选择详情页面
		Path.map("#detail").to(function () {
			//记录状态
			common.loadPageDiv("html/detail.html", true);
			window.history.replaceState({}, "title", "index.html")
		});
		//首页
		Path.map("#home").to(function () {
			//记录状态
			common.loadPageDiv("html/index.html");
		});
		// 收藏界面
		Path.map("#collectList").to(function () {
			//记录状态
			common.loadPageDiv("html/collectList.html", true);
		});
		// 我的界面
		Path.map("#mine").to(function () {
			//记录状态
			common.loadPageDiv("html/mine.html", true);
		});
		
		//查询结果列表点解进入页面
		Path.map("#productList/:status").to(function () {
			var type = this.params['status'];
			window.sessionStorage.searchType = type;
			common.loadPageDiv("html/productList.html", true);
		});

		//查询结果列表点解进入页面
		Path.map("#attachmentList").to(function () {
			common.loadPageDiv("html/attachmentList.html", true);
		});
		// 润滑油百科界面
		Path.map("#baike").to(function () {
			//记录状态
			common.loadPageDiv("html/baike.html", true);
		});
		
		// 经销商在线界面
		 Path.map("#pingtai").to(function () {
		 	//记录状态
		 	common.loadPageDiv("html/pingtai.html", true);
		 });
        //banner跳转
		Path.map("#banner_detail").to(function () {
			//记录状态
			common.loadPageDiv("html/banner_detail.html", true);
		});
		Path.listen();
	},
	
	/**
	 * 根据名称查询当前传递过来的行业
	 */
	getSelectIndustryInfoByName:function(industryName){
		//查询出来
		common.showLoading();
		var param={
			"cate_name":industryName	
		};
		var url = "/kmsetting/App/queryHyGxSbInfo";
		common.interfaceAjaxFunction(url,param,index.queryHyGxSbInfoSuccess);
	},
	/**
     * 根据当前用户，判断是否设置好行业，然后跳入不同页面
     */
	queryHyGxSbInfoSuccess:function(json){
    	var _this = this;
    	//成功时候
    	if(json.status == "0"){
    		if(json.data != undefined && json.data.length > 0){
    			//把数据保存在sessionStorge中，用于下
	    		window.sessionStorage.userSettingJson = JSON.stringify(json);
	    		//加载设置后的主页面
				kmMain.loadData();
    		}else{
    			//没有设置，显示问题
    			common.show_nodata("当前无行业信息", "knowledge-base-list");
    		}
    	}else{
    		//给出信息提示
    		//给出信息提示
			$("#knowledge-base-list").height($(window).height() - $(".header").height() - $("#indexPageSearchInput").height());
			common.show_error(json.message,"knowledge-base-list");
    	}
    	//转圈提示去除
    	common.hideLoading();
    },
	/**
	 * 取得用户的信息
	 */
	getUserInfo:function(){
		// window.localStorage.userid = "0001A210000000000AIL";
		var urlParam = common.getURLparams();
		//根据URL可以分三种情况 1.仅有userid  2.有userid和searchKey  3.有userid和industryName
		//查询key
		var searchKey = urlParam.searchKey;
		//行业信息
		var industryName = urlParam.industryName;
		//关键字查询知识点
		if(searchKey != undefined && searchKey != ""){
			window.sessionStorage.searchKey = searchKey;
		    //设置不显示
			window.sessionStorage.searchKeyNotBackFlag = "false";
			window.location.href = "#productList/search";
		}else if(industryName != undefined && industryName != ""){
			//行业不为空，支持查询行业显示数
			$(".set-up-img").hide();
			index.getSelectIndustryInfoByName(industryName);
		}
	},
	queryUser: function () {
		//调用根据ucode查询是否有此用户
		var userid=window.localStorage.userid;
		if(userid==undefined||userid=="" ){
			$("#indexPageMenuListContent").height($(window).height() - $(".header").height());
			$("#home-footer").hide();
			common.show_error("系统无此用户","indexPageMenuListContent");
			return;
		}
		var param = {
			"user_name": userid
		};
		//转圈
		//common.showLoading();
		var url = "/shellUser/queryPkByUsercode";
		common.interfaceAjaxFunction(url, param, index.queryUserSuccess, true);
    },
    /**
     * 根据当前用户，判断是否设置好行业，然后跳入不同页面
     */
    queryUserSuccess: function (json) {
		var _this = this;
		//成功时候
		if (json.status == "0") {
			if (json.data == undefined || json.data.length <= 0) {
				//加载设置页面
				//给出信息提示
				$("#indexPageMenuListContent").height($(window).height() - $(".header").height());
				$("#home-footer").hide();
				common.show_nodata("系统无此用户","indexPageMenuListContent");
			}
		} else {
			//给出信息提示
			$("#indexPageMenuListContent").height($(window).height() - $(".header").height());
			$("#home-footer").hide();
			common.show_error("系统无此用户","indexPageMenuListContent");
		}
		//转圈提示去除
		//common.hideLoading();
    },
    /**
     * each page init mothed
     */
    initialize: function () {
    	//异步加载swipe插件js
    	$script("js/lib/swiperSlide.min.js?v=" + common.version,function(){
    		//初始化轮播
		    var swiper = new Swiper('.swiper-container', {
		       /* nextButton: '.swiper-button-next',
		        prevButton: '.swiper-button-prev',*/
		        pagination: '.swiper-pagination',
		        paginationClickable: true,
		        // Disable preloading of all images
		        preloadImages: false,
		        // Enable lazy loading
		        lazyLoading: true,  
		        autoplay: 5000,
        		autoplayDisableOnInteraction: false
		    });
		    $(".swiper-slide").on("click",function(){
		    	//window.location.href = "#banner_detail";

				summer.openWin({
					id: 'banner_detail',
					showProgress:"true",
					url: 'http://h5web.bluemp.net/shellm/',
					type:'containner'
				});
		    });
    	});
		//附件长度
    	var data =  common.strToJson(window.localStorage.currentUserAttachDownloadFiles);
    	$(".enclosure-number").html(typeof(data) == "undefined"? "0" : data.length);
		//事件绑定
    	index.bindEvent();
		//路由绑定
		index.bindRout();
		//根据不同用户进入不同页面信息记录
		//index.getUserInfo();
		//index.queryUser();
		//初始化上拉加载下拉刷新控件
		common.initIscrollBody("indexPageMenuListContent");
		//
		common.hideLoading();
    }
};